<?php   if(!defined('BASEPATH')) exit('No direct script access allowed');
    class Login extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();
            $this->load->helper(array('form','url'));
            $this->load->database();
        }
        
        function index($data=null)
        {
            $this->load->view('dashboard/login_view',$data);
        }
        
        function validate_credentials()
        {
            if($this->input->post('submit')=='Login')
            {
                $this->load->library('form_validation');
                $config=array(
                            array(
                                'field'=>'username',
                                'label'=>'Account',
                                'rules'=>'trim|required|max_length[50]|xss_clean|callback_username_check'
                            ),
                            array(
                                'field'=>'password',
                                'label'=>'Password',
                                'rules'=>'trim|required|max_length[50]|xss_clean'
                            )
                );
                $this->form_validation->set_rules($config);
                if($this->form_validation->run()==FALSE)
                {
                    $this->index(); 
                }
                else
                {
                    $this->db->select('username,roles');
                    $this->db->from('tblmembership');
                    $this->db->where('username',$this->input->post('username'));
                    $this->db->where('password',md5($this->input->post('password')));
                    $query=$this->db->get();
                    if($query->num_rows()==0)
                    {
                        $data['error']='The account and password incorrect.';
                        $this->index($data);
                    }
                    else if($query->num_rows()==1)
                    {
                        $this->load->library('session');  
                        $this->session->unset_userdata('email');                  
                        $user=$query->row_array();
                        //set session
                        $newdata=array(
                                    'username' => $user['username'],
                                    'roles'    => $user['roles'],
                                    'islogged' => TRUE,
                        );
                        $this->session->set_userdata($newdata);
                        
                        redirect('dashboard/backend');
                    }
                }
            }
        }
        
        function username_check($str)
        {
            $this->db->select('username');
            $this->db->from('tblmembership');
            $this->db->where('username',$str);
            $query=$this->db->get();
            if($query->num_rows()==0)
            {
                $this->form_validation->set_message('username_check','The %s does not exist.');
                return FALSE;
            }
            else
            {
                return TRUE;
            }
        }
        
        function log_out()
        {
            $this->load->library('session');
            $this->session->sess_destroy();
            redirect('dashboard/login');
        }
    }
?>